/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.ta.web;

import java.io.*;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.ta.entity.StDynamicResource;
import com.thinkgem.jeesite.modules.ta.entity.StUserSign;
import com.thinkgem.jeesite.modules.ta.service.StAttachmentService;
import com.thinkgem.jeesite.modules.ta.service.StDynamicResourceService;
import com.thinkgem.jeesite.modules.ta.util.WebServiceUtil;

/**
 * 附件路径Controller
 * 
 * @author TTAO
 * @version 2017-06-05
 */
@Controller
@RequestMapping(value = "${adminPath}/ta/stDynamicResource")
public class StDynamicResourceController extends BaseController {

	public StDynamicResourceController() {
		// TODO 自动生成的构造函数存根
	}

	@Autowired
	private StDynamicResourceService stDynamicResourceService;
	@Autowired
	private StAttachmentService stAttachmentService;

	@ModelAttribute
	public StDynamicResource get(@RequestParam(required = false) String id) {
		StDynamicResource entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = stDynamicResourceService.get(id);
		}
		if (entity == null) {
			entity = new StDynamicResource();
		}
		return entity;
	}

	@RequestMapping(value = "list")
	public String list(String iid, Model model) {
		List<StDynamicResource> page = stDynamicResourceService
				.findListById(Long.parseLong(iid));
		model.addAttribute("iid", iid);
		model.addAttribute("page", page);
		return "modules/ta/stDynamicResourceList";
	}

	public String ascii2native(String theString) {
		char aChar;

		int len = theString.length();

		StringBuffer outBuffer = new StringBuffer(len);

		for (int x = 0; x < len;) {

			aChar = theString.charAt(x++);

			if (aChar == '\\') {

				aChar = theString.charAt(x++);

				if (aChar == 'u') {

					// Read the xxxx

					int value = 0;

					for (int i = 0; i < 4; i++) {

						aChar = theString.charAt(x++);

						switch (aChar) {

						case '0':

						case '1':

						case '2':

						case '3':

						case '4':

						case '5':

						case '6':
						case '7':
						case '8':
						case '9':
							value = (value << 4) + aChar - '0';
							break;
						case 'a':
						case 'b':
						case 'c':
						case 'd':
						case 'e':
						case 'f':
							value = (value << 4) + 10 + aChar - 'a';
							break;
						case 'A':
						case 'B':
						case 'C':
						case 'D':
						case 'E':
						case 'F':
							value = (value << 4) + 10 + aChar - 'A';
							break;
						default:
							throw new IllegalArgumentException(
									"Malformed   \\uxxxx   encoding.");
						}

					}
					outBuffer.append((char) value);
				} else {
					if (aChar == 't')
						aChar = '\t';
					else if (aChar == 'r')
						aChar = '\r';

					else if (aChar == 'n')

						aChar = '\n';

					else if (aChar == 'f')

						aChar = '\f';

					outBuffer.append(aChar);

				}

			} else
				outBuffer.append(aChar);
		}
		return outBuffer.toString();
	}

	@RequestMapping(value = "down")
	public void down(String filename, String iid, HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		byte[] buffer = WebServiceUtil.attService(iid, request);
		filename = ascii2native(filename);
		response.setContentType("application/x-msdownload");
		String iso_filename = parseGBK(filename);
		response.setHeader("Content-Disposition", "attachment;filename="
				+ iso_filename);
		ServletOutputStream op = response.getOutputStream();
		op.write(buffer);
		op.flush();
		op.close();

	}

	public static String parseGBK(String sIn) {
		if (sIn == null || sIn.equals(""))
			return sIn;
		try {
			return new String(sIn.getBytes("GBK"), "ISO-8859-1");
		} catch (UnsupportedEncodingException usex) {
			return sIn;
		}
	}

	@RequestMapping(value = "delOne")
	public String del(String aid, String nid, HttpServletRequest request) {
		stDynamicResourceService.StDynamicResourceDelOne(aid);
		stAttachmentService.StAttachmentDelOne(aid);
		return "redirect:" + Global.getAdminPath()
				+ "/ta/stDynamicResource/list?iid=" + nid;
	}

	@RequestMapping(value = "delAll")
	public String delAll(String iid, HttpServletRequest request) {
		stDynamicResourceService.StDynamicResourceDelAll(Long.parseLong(iid));
		stAttachmentService.StAttachmentDelAll(Long.parseLong(iid));
		return "redirect:" + Global.getAdminPath() + "/ta/stWeihu/?repage";
	}

	@RequestMapping(value = "upload")
	public String upload(String iid, HttpServletRequest request) {
		return "redirect:http://localhost/webservice2/EasyUiPage/MutiFilesUpload.aspx?iid="
				+ iid + "&dbtable=st_attachment&dbfield=data&dbfilefield=NAME";
	}

	@RequestMapping(value = "updateName")
	public String updateName(String path, String iid, String nid,
			HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (path != null) {
			map.put("path", path.toString());
		}
		map.put("resValue", iid.trim().toString());
		stDynamicResourceService.StDynamicResourceUpdateName(map);
		return "redirect:" + Global.getAdminPath()
				+ "/ta/stDynamicResource/list?iid=" + nid;
	}

	@RequiresPermissions("ta:stDynamicResource:view")
	@RequestMapping(value = "form")
	public String form(StDynamicResource stDynamicResource, Model model) {
		model.addAttribute("stDynamicResource", stDynamicResource);
		return "modules/ta/stDynamicResourceForm";
	}

	@RequiresPermissions("ta:stDynamicResource:edit")
	@RequestMapping(value = "save")
	public String save(StDynamicResource stDynamicResource, Model model,
			RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, stDynamicResource)) {
			return form(stDynamicResource, model);
		}
		stDynamicResourceService.save(stDynamicResource);
		addMessage(redirectAttributes, "保存附件路径成功");
		return "redirect:" + Global.getAdminPath()
				+ "/ta/stDynamicResource/?repage";
	}

	@RequiresPermissions("ta:stDynamicResource:edit")
	@RequestMapping(value = "delete")
	public String delete(StDynamicResource stDynamicResource,
			RedirectAttributes redirectAttributes) {
		stDynamicResourceService.delete(stDynamicResource);
		addMessage(redirectAttributes, "删除附件路径成功");
		return "redirect:" + Global.getAdminPath()
				+ "/ta/stDynamicResource/?repage";
	}

}